﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="DHGMS Solutions" file="DataTable.cs">
//   2004-2012 DHGMS Solutions. Some Rights Reserved. Licensed under GNU General Public License version 2 (GPLv2)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.TplHelper.Controller.Task.SqlBulkCopy
{
    using System;
    using System.Data.SqlClient;

    using Dhgms.TplHelper.Model.Info;

    /// <summary>
    /// Base class for an SQL Bulk Import Task that uses a Data Table as its source
    /// </summary>
    public class DataTable : Base
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="DataTable"/> class. 
        /// </summary>
        /// <param name="taskName">
        /// Name of the data being bulk imported
        /// </param>
        /// <param name="destinationConnectionString">
        /// Connection String for the target database
        /// </param>
        /// <param name="destinationDatabase">
        /// Name of the database the table sits in
        /// </param>
        /// <param name="destinationSchema">
        /// the schema the destination table sits in
        /// </param>
        /// <param name="destinationTable">
        /// the table the data is going to end up in
        /// </param>
        /// <param name="targetTableDeleteAction">
        /// the delete action to be taken on the target table
        /// </param>
        protected DataTable(
            string taskName, 
            string destinationConnectionString,
            string destinationDatabase,
            string destinationSchema, 
            string destinationTable, 
            DestinationTableDeleteAction targetTableDeleteAction)
            : base(taskName, destinationConnectionString, destinationDatabase, destinationSchema, destinationTable, targetTableDeleteAction)
        {
        }

        #endregion

        #region Methods

        /// <summary>
        /// Carries out the bulk copy from the data table
        /// </summary>
        /// <param name="table">
        /// Data Table to copy data from
        /// </param>
        protected void ActOnData(System.Data.DataTable table)
        {
            using (var bulkCopy = new SqlBulkCopy(this.DestinationConnectionString, SqlBulkCopyOptions.TableLock))
            {
                bulkCopy.DestinationTableName = "[" + this.DestinationSchema + "].[" + this.DestinationTable + "]";
                bulkCopy.BulkCopyTimeout = 0;

                bulkCopy.WriteToServer(table);
            }
        }

        /// <summary>
        /// Fetches the data and passes it on to be bulk inserted
        /// </summary>
        protected override void OnFetchAndProcessSourceData()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}